package game.gamescene
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import game.physic.Input;
	import game.physic.Physic;
	
	public class GameScene extends Sprite
	{
		public var strip:MovieClip;
		public var hand:MovieClip;
		public var powerMeter:MovieClip;
		public var itemsContainer:MovieClip;
		public var enemiesContainer:MovieClip;
		public var debug:MovieClip;
		
		// CONST //////////////////////////////////////
		static public const STRIP_HEIGHT:Number = 370.0;
		
		static public const SCENE_CENTER_X:Number = 385.0;
		static public const SCENE_CENTER_Y:Number = 198.0;
		static public const SCENE_CORRIDOR_BASE:Number   = 350.0;
		static public const SCENE_CORRIDOR_HEIGHT:Number = 150.0;
		
		
		public var handIsReady:Boolean = true;
		protected var _handLastFrame:int = 0;
		
		public function GameScene()
		{
			super();
		}
		
		public function verifyHandState ():void 
		{
		}
		
		public function getHandPosition():Point
		{

			return new Point( hand.x, hand.y);
		}
		
		public function updatePowerMeter ( ):void
		{
			var rot:Number = 122*(Ref.model.powerLevel - 1.0) + 122.0;
			TweenLite.to(powerMeter.needle, .2, {rotation:rot});
		}
		
		public function updateHandPosition ():void
		{
			var v1:Number = Math.max ( 70, Input.mouseX );
			var v2:Number = Math.min ( v1, Ref.model.screenWidth - 70 );
			TweenLite.to(hand, .1, {x:v2});
			
			if ( v2 < Ref.model.screenWidth / 2.0 + 30 )
				hand.scaleX = 1.0;
			else
				hand.scaleX = -1.0;
		}
		
		public function showHandHit ():void
		{
			hand.hand.gotoAndPlay("hit");
			handIsReady = false;
			TweenLite.delayedCall(.2, function ():void {handIsReady = true;} );
		}
		
		public function showHandPrepared ():void
		{
			hand.hand.gotoAndPlay("prepared");
		}
		
		public function showHandIdle ():void
		{
			hand.hand.gotoAndPlay("idle");
		}
		
		// ITEMS //////////////////////////////////////////////////////////
		public function createNewItem():void
		{
			//trace("createNewItem");
			
			var itemDataList:XMLList = XMLList(Ref.model.configXML..item);
			var totalItems:int       = itemDataList.length();
			var index:int            = int(Math.random() * totalItems);
			
			var itemData:XML = itemDataList[index];
			
			var item:GameItem = new GameItem(itemData);
			item.x = Ref.model.screenWidth + item.width/2 + 20;

			if ( itemData.@height != undefined && itemData.@height != null )
				item.y = STRIP_HEIGHT - Number( itemData.@height);
			else
				item.y = STRIP_HEIGHT - item.height;
			
			item.alpha = 0;

			Ref.view.stripItems.push(item);
			itemsContainer.addChild(item);
			
			TweenLite.to(item, .3, {alpha:1});
		}
		
		public function removeExtraItems():void
		{
			if (  !Ref.view.stripItems ) return;
			if (  Ref.view.stripItems.length <= 0 ) return;
			
			var item:DisplayObject = Ref.view.stripItems[0] as DisplayObject;
				
			if ( item.x + item.width < 0 ) 
			{
				item.parent.removeChild(item);
				Ref.view.stripItems.shift();
			}
		}
		
		public function updateItemsPosition():void
		{
			for ( var i:int = 0 ; i < Ref.view.stripItems.length ; i++)
			{
				var item:DisplayObject = Ref.view.stripItems[i] as DisplayObject;
				
				item.x -= Ref.model.stripSpeed;
			}
		}
		
		/// enemies ////////////////////////////////////////////////////////
		public function createNewEnemy():void
		{
			trace("createNewEnemy");
			
			var dataList:XMLList = XMLList(Ref.model.configXML..enemy);
			var total:int        = dataList.length();
			var index:int        = int(Math.random() * total);
			
			var data:XML = dataList[index];
			
			var enemy:GameEnemy = new GameEnemy(data);
	
			enemy.setPositionAndDistance(0.0);

			var bPhy:b2Body = Ref.model.physic.createEnemy(enemy.xFactor, data);
			bPhy.SetUserData( enemy );
			enemy.phBody = bPhy;
						
			Ref.view.enemies.push(enemy);
			enemiesContainer.addChildAt(enemy , 0);
		}
		
		public function updateEnemyPosition():void
		{
			var hmid:Number = (Physic.HEIGHT-24.0)/2.0;
			
			for ( var i:int = 0; i < Ref.view.enemies.length; i++ )
			{
				var enemy:GameEnemy =  Ref.view.enemies[i] as GameEnemy;
				
				//trace("xf: " + i + ": " + enemy.xFactor);
				
				var f:Number = -(enemy.phBody.GetPosition().y*Physic.m_physScale - 12 - hmid) / hmid;
				enemy.xFactor = Math.max( Math.min(1.0,f),-1.0);
//				trace("xf2: " + i + ": " + enemy.xFactor + ", "+ enemy.phBody.GetPosition().y*Physic.m_physScale);
				var dist:Number = (enemy.phBody.GetPosition().x*Physic.m_physScale - 5 )/ Physic.WIDTH; 
				enemy.setPositionAndDistance(dist*dist);
			}
		}
		
		public function updateHitItemsPosition():void
		{
			var hmid:Number = (Physic.HEIGHT-24.0)/2.0;
			
			for ( var i:int = 0; i < Ref.view.hitItems.length; i++ )
			{
				var item:GameItem =  Ref.view.hitItems[i] as GameItem;
				
				var f:Number = -(item.phBody.GetPosition().y*Physic.m_physScale - 12 - hmid) / hmid;
				item.xFactor = Math.max( Math.min(1.0,f),-1.0);
				//				trace("xf2: " + i + ": " + enemy.xFactor + ", "+ enemy.phBody.GetPosition().y*Physic.m_physScale);
				var dist:Number = (item.phBody.GetPosition().x*Physic.m_physScale - 5 )/ Physic.WIDTH; 
				item.setPositionAndDistance(dist*dist);
			}
		}

		public function hitItem( item:Sprite, index:int):void
		{
			trace("hitItem !!");
			Ref.view.stripItems.splice(index,1);
			Ref.view.hitItems.push(item);
			item.parent.removeChild(item);
			Ref.view.gameScene.enemiesContainer.addChild(item);
			
			var hmid:Number = Ref.model.screenWidth/2.0;
			var f:Number = -(item.x + item.width/2.0 - hmid)/hmid;
			var bPhy:b2Body = Ref.model.physic.createHitItemBody( f );
			bPhy.SetUserData( item );
			GameItem(item).phBody = bPhy;
			
		}
	}
}